package uk.co.agileworks.bidpirate.web;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import uk.co.agileworks.bidpirate.model.Bid;
import uk.co.agileworks.bidpirate.service.Pager;

@Controller
public final class ListBidsController extends ControllerHelper {

	static final String REDIRECT = "redirect:/listBids.html";

	@RequestMapping("/listBids_deleteBid.html")
	public String deleteBid(@RequestParam("id") long id) {
		getBusinessLogic().deleteBid(id);
		return REDIRECT;
	}

	@RequestMapping("/listBids_updateItem.html")
	public String updatePrice(@RequestParam("bidId") long bidId) {
		getBusinessLogic().updateItem(bidId);
		return REDIRECT;
	}

	@RequestMapping("/listBids.html")
	public void listBids(Model model, @RequestParam(value = "page", required = false) Integer page) {
		List<Bid> bids = getBusinessLogic().listBids(getUserSession().getUser());
		Pager pager = new Pager(bids);
		pager.setCurrentPage(getUserSession().currentPage(page));
		model.addAttribute(pager);
		model.addAttribute("openItemUrl", getOpenItemUrl());
	}

}
